<template>
	<public-view navType="other" navTitle="设置支付密码">
		<view class="content">
			<view class="password-input-box f-s-24 flex a-i-c">
				<view class="title">
					支付密码
				</view>
				<input class="ihnput" v-model="password" maxlength="6" :password="true" style="flex: 1;" type="number" placeholder="请输入支付密码">
			</view>
			<view class="password-input-box f-s-24 flex a-i-c" style="margin-top: 20rpx;">
				<view class="title">
					验证码
				</view>
				<input class="ihnput" v-model="code" style="flex: 1;" type="text" placeholder="请输入验证码">
				<view class="color-2567F3 f-s-24" style="text-align: center;" @click="getSmsCode">
					{{back_time >= 60?"获取验证码":back_time+'s'}}
				</view>
			</view>
			<view class="enter-btn all-center f-s-30 color-fff" @click="changePassword">
				确认密码
			</view>
		</view>
	</public-view>
</template>

<script>
	import {mapGetters} from "vuex"
	import {goBack} from "@/nav.js"
	export default {
		data() {
			return {
				code:"",
				password:"",
				back_time:60,
				interval:"",
				timeout:""
			};
		},
		computed:{
			...mapGetters(["userInfo"])
		},
		methods:{
			changePassword(){
				let that = this
				if(that.code === ''){
					uni.showToast({
						title:"请输入验证码",
						icon:"none"
					})
					return
				}
				if(that.password.length<6){
					uni.showToast({
						title:"请输入6位支付密码",
						icon:"none"
					})
					return
				}
				that.util.postRequest({
					url:"v1/user/setPayPass",
					data:{
						code:that.code,
						pay_pass:that.password
					}
				}).then(res=>{
					that.$getUser()
					uni.showToast({
						title:"设置成功",
						duration:2500
					})
					let timeout=setTimeout(()=>{
						clearTimeout(timeout)
						goBack()
					},2500)
				}).catch(e=>{
					console.log(e)
					uni.showToast({
						title:e.msg||"请求失败",
						icon:"none"
					})
				})
			},
			getSmsCode(){
				let that = this
				if(that.backTime<60){
					return
				}
				if(that.timeout !==""){
					uni.showToast({
						title:"操作频繁，请稍后重试",
						icon:"none"
					})
					return
				}else{
					that.timeout = setTimeout(()=>{
						that.timeout = ""
						clearTimeout(that.timeout)
					},1000)
				}
				console.log(that.timeout)
				uni.showLoading({
					title:"发送中...",
					mask:true
				})
				that.util.postRequest({
					url:"v1/auth/sendSms",
					data:{
						mobile:that.userInfo.mobile
					}
				}).then(res=>{
					console.log(res)
					uni.hideLoading()
					that.backTime()
				}).catch(e=>{
					uni.hideLoading()
				})
			},
			backTime(){
				let that = this
				if(that.interval){
					return
				}
				that.interval = setInterval(()=>{
					that.back_time--
					if(that.back_time<=0){
						clearInterval(that.interval)
						that.interval = ''
						that.back_time = 60
					}
				},1000)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.enter-btn{
		width: 702upx;
		height: 96upx;
		background: #2567F3;
		border-radius: 128upx;
		margin-top: 34upx;
	}
	.password-input-box{
		width: 100%;
		padding: 42upx 40upx;
		background: #FFFFFF;
		border-radius: 20px;
		.ihnput{
			line-height: 34upx;
		}
		.title{
			line-height: 40upx;
			margin-right: 36upx;
		}
	}
	.content{
		width: 750upx;
		padding: 42upx 30upx;
	}
</style>
